<template>
	<view>
		<!-- 0，头部标题组件 -->
		<group-head @gotoBack="gotoBack" @removeUsers="removeUsers"></group-head>

		<!-- 1，主要内容 -->
		<view class="user-data">
			<!-- 1.1，族长信息 -->
			<view class="patriarch">
				<view class="top-title">
					<text>族长</text>
				</view>
				<view class="user-box">
					<view class="box-con">
						<view class="left-img">
							<image :src="patriarchData.head"></image>
						</view>
						<view class="user-nickname">
							<text>{{patriarchData.nickname}}</text>
						</view>
					</view>
				</view>
			</view>
			<!-- 1.2，管理员信息 -->
			<view class="admin-data">
				<view class="top-title">
					<text>管理员</text>
				</view>
				<block v-for="(item,index) in adminData" :key="index">
					<my-user-item :isShowRadio="isShowRadio" :userData="item" @clickRadio="removeClickRadio">
					</my-user-item>
				</block>

			</view>
		</view>

		<!-- 2，添加管理员 -->
		<view class="add-admin">
			<view class="admin-box">
				<view class="add-img" @click="showAddAdmin">
					<image :src="require('@/static/img/msg/group/add.svg')"></image>
				</view>
				<view class="text">
					<text>添加管理员</text>
				</view>
			</view>
		</view>

		<!-- 3，底部弹框 -->
		<group-animation @addUserAdmin="addUserAdmin" @display="display" :isShow="isShow" :usersData="usersData"
			@clickRadio="clickRadio"></group-animation>
	</view>
</template>

<script>
	//1，引入网络请求
	import {
		queryGroupUsers,
		changeUsersAdmin
	} from '@/api/http/msg/group.js'

	//2，引入子组件
	import groupHead from './child/group-head.vue'
	import groupAnimation from './child/group-animation.vue'

	import myUserItem from '@/components/content/my-user-item/my-user-item.vue'

	export default {
		components: {
			groupHead,
			groupAnimation,

			myUserItem
		},
		data() {
			return {
				groupId: '',
				//普通管理员信息
				adminData: [],
				//族长信息
				patriarchData: {},
				//普通用户
				usersData: [],

				//是否弹出框
				isShow: false,
				//添加权限的用户
				addUserData: [],
				isShowRadio: true,
				removeUserData: []
			}
		},
		methods: {
			//0，返回上一页
			gotoBack() {
				uni.navigateBack({
					delta: 1,
				})
				// uni.reLaunch({
				// 	url: '/subpkg/msg/set/set-group/set-group-admin/set-group-admin?groupId=' +
				// 		this.groupId
				// })
			},

			//1，查询成员信息
			queryGroupUsers(groupId, groupName, isAdmin) {
				queryGroupUsers(groupId, groupName, isAdmin).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					if (isAdmin == '1') {
						var users = res.data.busInfo
						//数据处理
						//取出族长用户
						var zzData = {}
						for (var i = 0; i < users.length; i++) {
							if (users[i].types == '1') {
								zzData = users[i]
								users.splice(i, 1)
							}
						}

						this.patriarchData = zzData
						this.adminData = users

						for (var i = 0; i < this.adminData.length; i++) {
							this.adminData[i].isChecked = false
						}
					} else {
						var usersData = res.data.busInfo
						for (var i = 0; i < usersData.length; i++) {
							usersData[i].isChecked = false
						}
						this.usersData = usersData
					}

				})
			},
			//2，添加管理员
			showAddAdmin() {
				this.isShow = true
				this.queryGroupUsers(this.groupId, '', '2')
			},
			//3，添加用户选中
			clickRadio(item) {
				for (var i = 0; i < this.usersData.length; i++) {
					if (this.usersData[i].userId == item.userId) {
						this.usersData[i].isChecked = !this.usersData[i].isChecked
						//如果是选中则加入到参数中
						if (this.usersData[i].isChecked) {
							this.addUserData.push(this.usersData[i])
							return
						} else {
							//判断是否已经添加
							for (var j = 0; j < this.addUserData.length; j++) {
								if (this.addUserData[j].userId == this.usersData[i].userId) {
									this.addUserData.splice(j, 1)
									return
								}
							}
						}
					}
				}
			},
			//4，关闭弹框
			display() {
				this.isShow = false
			},
			//5，给用户添加权限
			addUserAdmin() {
				if(this.addUserData.length == 0){
					return
				}
				changeUsersAdmin(this.addUserData, '2', this.groupId).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					uni.$showMsg(res.data.busInfo);
					this.queryGroupUsers(this.groupId, '', '1')
				})
				this.isShow = false
				this.addUserData = []
			},
			//6，移除用户选中
			removeClickRadio(item) {
				for (var i = 0; i < this.adminData.length; i++) {
					if (this.adminData[i].userId == item.userId) {
						this.adminData[i].isChecked = !this.adminData[i].isChecked
						//如果是选中则加入到参数中
						if (this.adminData[i].isChecked) {
							this.removeUserData.push(this.adminData[i])
							return
						} else {
							//判断是否已经添加
							for (var j = 0; j < this.removeUserData.length; j++) {
								if (this.removeUserData[j].userId == this.adminData[i].userId) {
									this.removeUserData.splice(j, 1)
									return
								}
							}
						}
					}
				}
			},
			//7，移除用户管理员身份
			removeUsers() {
				if(this.removeUserData.length == 0){
					return
				}
				changeUsersAdmin(this.removeUserData, '3', this.groupId).then(res => {
					if (res.data.pubInfo.code !== 0) {
						return uni.$showMsg(res.data.pubInfo.message);
					}
					uni.$showMsg(res.data.busInfo);
					this.queryGroupUsers(this.groupId, '', '1')
				})
				this.removeUserData = []
			}

		},
		onLoad(e) {
			this.groupId = e.groupId
			this.queryGroupUsers(this.groupId, '', '1')
		}
	}
</script>

<style lang="scss" scoped>
	.top-title {
		height: 60rpx;
		width: 100%;
		background-color: rgba(229, 229, 229, 1);
		color: rgba(166, 166, 166, 1);
		font-size: 32rpx;
		line-height: 60rpx;

		text {
			margin-left: 30rpx;
		}
	}

	.user-box {
		width: 100%;
		height: 100rpx;

		&:active {
			background-color: #F2F2F2;
		}

		.box-con {
			width: 94%;
			margin-left: 3%;
			display: flex;

			.left-img {
				image {
					margin-top: 10rpx;
					width: 80rpx;
					height: 80rpx;
					border-radius: 50%;
				}
			}

			.user-nickname {
				line-height: 100rpx;

				text {
					margin-left: 30rpx;
				}
			}
		}
	}


	.add-admin {

		height: 100rpx;

		.admin-box {
			display: flex;
			width: 94%;
			margin-left: 3%;
		}

		.add-img {
			margin-top: 10rpx;
			width: 80rpx;
			height: 80rpx;
			border-radius: 50%;
			background-color: rgba(229, 229, 229, 1);
			text-align: center;

			image {
				margin-top: 10rpx;
				width: 60rpx;
				height: 60rpx;
			}
		}

		.text {
			line-height: 100rpx;

			text {
				margin-left: 30rpx;
				font-size: 34rpx;
			}
		}
	}
</style>
